#include <iostream>
#include <QSettings>
#include "statusdisplay.h"
#include "ui_statusdisplay.h"

statusDisplay::statusDisplay(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::statusDisplay)
{
    ui->setupUi(this);

}

statusDisplay::~statusDisplay()
{
    delete ui;
}

void statusDisplay::on_startButton_clicked()
{
    ui->stopButton->setEnabled(true);
    ui->startButton->setEnabled(false);
    ui->serialPortLine->setEnabled(false);
    ui->serialSpeedLine->setEnabled(false);
    //ui->tipSlider->setValue(ui->tipSlider->value()+15);

    //start serial stream
    emit startSerial();             //if it worked do nothing else
    //I need to do something to check that the serial stream start successfully
    //on_stopButton_clicked();   //if it failed reset with the stop button
}

void statusDisplay::on_stopButton_clicked()
{
    ui->stopButton->setEnabled(false);
    ui->startButton->setEnabled(true);
    ui->serialPortLine->setEnabled(true);
    ui->serialSpeedLine->setEnabled(true);

    //stop serial stream
    emit stopSerial();
}

void statusDisplay::on_quitButton_clicked()
{
    this->close();
}

void statusDisplay::on_tipSlider_valueChanged(int value)
{
    ui->tipSpinBox->setValue(value*360/255);
}

void statusDisplay::on_middleSlider_valueChanged(int value)
{
    ui->middleSpinBox->setValue(value*360/255);

}

void statusDisplay::on_knuckleSlider_valueChanged(int value)
{
    ui->knuckleSpinBox->setValue(value*360/255);

}

void statusDisplay::on_sweepSlider_valueChanged(int value)
{
    ui->sweepSpinBox->setValue(value*360/255);

}

void statusDisplay::on_openSlider_valueChanged(int value)
{
    ui->openSpinBox->setValue(value*360/255);

}

void statusDisplay::tipSet(char value)
{
    //value is 0 to 255
    ui->tipSlider->setValue(value);
}

void statusDisplay::middleSet(char value)
{
    //value is 0 to 255
    ui->middleSlider->setValue(value);
}

void statusDisplay::knuckleSet(char value)
{
    //value is 0 to 255
    ui->knuckleSlider->setValue(value);
}

void statusDisplay::sweepSet(char value)
{
    //value is 0 to 255
    ui->sweepSlider->setValue(value);
}

void statusDisplay::openSet(char value)
{
    //value is 0 to 255
    ui->openSlider->setValue(value);
}

void statusDisplay::on_serialPortLine_textChanged(const QString &arg1)
{
    //set the value of portName here
    //char portName[arg1.size()+1];  // = new char(arg1.size()+1); // = arg1.toStdString().c_str();;
    //strcpy(portName,arg1.toStdString().c_str());
    emit portNameSet(arg1);   //portName);
}

void statusDisplay::on_serialSpeedLine_textChanged(const QString &arg1)
{
    emit baudSet(arg1.toLong());
}

void statusDisplay::readSettings()
{
    //QSettings settings("Distal Touch Group", "Status Display");

    //emit portNameSet(settings.value("port", "dev/ttyACM0").);
}

void statusDisplay::writeSettings()
{
    QSettings settings("Distal Touch Group", "Status Display");

    //settings.setValue("geometry", geometry());
    settings.setValue("port", ui->serialPortLine->text());
    settings.setValue("baud", ui->serialSpeedLine->text()); //not actually used yet
//    settings.setValue();
//    settings.setValue();
//    settings.setValue();
//    settings.setValue();
//    settings.setValue();
}

void statusDisplay::logMessage(QString message)
{
    //print a message to the serial log
    ui->serialLogText->append(message);
}

void statusDisplay::serialfailure()
{
    ui->stopButton->setEnabled(false);
    ui->startButton->setEnabled(true);
    ui->serialPortLine->setEnabled(true);
    ui->serialSpeedLine->setEnabled(true);
}

void statusDisplay::on_pushButton_clicked()
{
    ui->serialLogText->clear();
}
